package com.mazaiting.easy.base.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import androidx.appcompat.widget.Toolbar;
import com.mazaiting.easy.R;
import com.mazaiting.easy.base.presenter.BasePresenter;

/**
 * 带有Toolbar的Activity
 * @author mazaiting
 * @date 2018/3/23
 */
public abstract class BaseToolbarActivity<T extends BasePresenter> extends BaseLoadingActivity<T>{
    /**导航栏*/
    private Toolbar mToolbar;

    /**
     * 如果重写此方法，在方法体内先调用父类的bindView方法
     * @param view 视图
     * @param savedInstanceState bundle
     */
    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        super.bindView(view, savedInstanceState);
        // 获取 Toolbar
        mToolbar = view.findViewById(R.id.toolbar);
        // 检测是否为空
        if (null == mToolbar) {
            throw new NullPointerException("Please use Toolbar in your layout file.");
        }
        // 设置导航栏
        setSupportActionBar(mToolbar);
        // 设置返回按钮
        if (null != getSupportActionBar()) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 返回按钮
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }
}
